/**
 * @Company 航天科技山西公司
 * @Project hitech-WORKFLOW-Web
 * @Package com.hitech.web.workflow
 * @Title ActModelController.java
 * @Description TODO(描述)
 * @author 颜金星
 * @create 2017年4月21日-下午2:52:29
 * @version V 1.0
 */
package com.casic.modules.activiti.controller;
import com.casic.common.utils.PageUtils;
import com.casic.common.utils.R;
import com.casic.modules.activiti.entity.HistoricActivity;
import com.casic.modules.activiti.entity.ProcessDefinition;
import com.casic.modules.activiti.entity.ProcessInstance;
import com.casic.modules.activiti.entity.WorkFlowConfig;
import com.casic.modules.activiti.service.ActFlowService;
import com.casic.modules.activiti.util.JqGridPageView;
import com.casic.modules.activiti.util.WebUtil;
import com.casic.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Company 航天科技山西公司
 * @Project hitech-WORKFLOW-Web
 * @Package com.hitech.web.workflow
 * @ClassName ActModelController.java
 * @Description TODO(工作流flow控制类)
 * @author 颜金星
 * @create 2017年4月21日-下午2:52:29
 */
@RestController
@Api(value = "流程管理类", description = "流程管理类")
@RequestMapping(value = "/workflow/actFlow", method = RequestMethod.POST)
public class ActFlowController extends AbstractController {
      
	  @Autowired
	  private ActFlowService actFlowService;
	  
	  @Autowired
	  private WorkFlowConfig workFlowConfig;
	  /**
	   * 
	   * @Title getActModelList
	   * @Description (查询流程定义列表)
	   * @author 颜金星
	   * @create 2017年5月11日-下午2:53:27
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "流程列表")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/getFlowPage",method = RequestMethod.GET)
	  public Object getActModelList(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) {
		  Map<String, Object> params = WebUtil.getParameterMap(request);
		  int firstResult = 1;
		  int maxResults = 10;
		  if(params.get("page")!=null){
			  firstResult=Integer.parseInt((String)params.get("page"));
		  }
		  if(params.get("limit")!=null){
			  maxResults = Integer.parseInt((String)params.get("limit"));
		  }
		  if(!params.containsKey("rows")){
			  params.put("rows",Integer.parseInt((String)params.get("limit")));
		  }
		    JqGridPageView<ProcessDefinition> page=actFlowService.findFlowByPage(params);
		  int pagetotal=(int)page.getRecords();

		  PageUtils pageUtil = new PageUtils(page.getRows(), pagetotal, maxResults, Integer.parseInt(params.get("page")+""));

		  return R.ok().put("page", pageUtil);

	  }
	  /**
	   *
	   * @Title deoploymentForClasspath
	   * @Description (流程定义)
	   * @author 颜金星
	   * @create 2017年5月11日-下午2:54:54
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "流程定义")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/deoploymentForClasspath")
	  public R deoploymentForClasspath(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) {
		    Map<String, Object> params = WebUtil.getParameterMap(request);
		    Deployment deployment=actFlowService.deoploymentForClasspath(params);

		  params.put("deployment",deployment);
		  return R.ok(params);
	  }
	  /**
	   * 
	   * @Title deoploymentForZip
	   * @Description (流程定义)
	   * @author 颜金星
	   * @create 2017年5月11日-下午2:57:51
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "流程定义")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/deoploymentForZip")
	  public R deoploymentForZip(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) {
		    Map<String, Object> params = WebUtil.getParameterMap(request);
		    Deployment deployment=actFlowService.deoploymentForZip(params);
		  params.put("deployment",deployment);
		  return R.ok(params);
	  }
	  /**
	   * 
	   * @Title delteProcessDefinitionByKey
	   * @Description (级联删除流程)
	   * @author 颜金星
	   * @create 2017年5月11日-下午3:31:51
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "流程删除")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/delteProcessDefinitionByKey/{key}")
	  public R delteProcessDefinitionByKey(HttpServletRequest request, @PathVariable("key")String key, HttpServletResponse response) {
		    Map<String, Object> params = WebUtil.getParameterMap(request);
		    params.put("key",key);
		    actFlowService.delteProcessDefinitionByKey(params);
		  return R.ok();
	  }
	  /**
	   * 
	   * @Title startProcessInstance
	   * @Description (启动流程实例)
	   * @author 颜金星
	   * @create 2017年5月11日-下午4:01:57
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "启动流程实例")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/startProcessInstance")
	  public R startProcessInstance(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) {
		    Map<String, Object> params = WebUtil.getParameterMap(request);
		    String formKey=actFlowService.startProcessInstance(params);
		  params.put("formKey",formKey);
		  return R.ok(params);
	  }
	  /**
	   * 
	   * @Title startProcessInstanceByForm
	   * @Description (获取启动流程表单数据)
	   * @author 颜金星
	   * @create 2017年5月12日-下午4:26:13
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "findProcessForm")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/findProcessForm",method = RequestMethod.GET)
	  public R findProcessForm(HttpServletRequest request, String processDefinitionId, HttpServletResponse response) {
		    Map<String, Object> params = WebUtil.getParameterMap(request);
		    params.put("processDefinitionId",processDefinitionId);
		    Map<String, Object> data=actFlowService.findProcessForm(params);
		  params.put("data",data);
		  return R.ok(params);
	  }
	  /**
	   * 
	   * @Title startForm
	   * @Description (启动动态表单流程实例)
	   * @author 颜金星
	   * @create 2017年5月12日-下午5:02:45
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @return Object
	   * @throws
	   */

	  @ApiOperation(value = "启动动态表单流程实例")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/startProcessByForm")
	  public R startForm(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) {
		    Map<String, Object> params = WebUtil.getParameterMap(request);
		    actFlowService.startProcessByForm(params,request);
		  return R.ok(params);
	  }
	  /**
	   * 
	   * @Title getWorkFlowRunning
	   * @Description (查询运行中流程实例)
	   * @author 颜金星
	   * @create 2017年5月15日-下午2:56:40
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "查询运行中流程实例")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/getWorkFlowRunning",method = RequestMethod.GET)
	  public R getWorkFlowRunning(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) {
		  Map<String, Object> params = WebUtil.getParameterMap(request);
		  int firstResult = 1;
		  int maxResults = 10;
		  if(params.get("page")!=null){
			  firstResult=Integer.parseInt((String)params.get("page"));
		  }
		  if(params.get("limit")!=null){
			  maxResults = Integer.parseInt((String)params.get("limit"));
		  }
		  if(!params.containsKey("rows")){
			  params.put("rows",Integer.parseInt((String)params.get("limit")));
		  }
		    JqGridPageView<ProcessInstance> page=actFlowService.getWorkFlowRunning(params);
		  int pagetotal=(int)page.getRecords();

		  PageUtils pageUtil = new PageUtils(page.getRows(), pagetotal, maxResults, Integer.parseInt(params.get("page")+""));

		  return R.ok().put("page", pageUtil);
	  }
	  /**
	   * 
	   * @Title finished
	   * @Description (查询以结束流程实例)
	   * @author 颜金星
	   * @create 2017年5月15日-下午4:36:04
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "查询以结束流程实例")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/finished",method = RequestMethod.GET)
	  public R finished(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) {
		  Map<String, Object> params = WebUtil.getParameterMap(request);
		   int firstResult = 1;
		  int maxResults = 10;
		  if(params.get("page")!=null){
			  firstResult=Integer.parseInt((String)params.get("page"));
		  }
		  if(params.get("limit")!=null){
			  maxResults = Integer.parseInt((String)params.get("limit"));
		  }
		  if(!params.containsKey("rows")){
			  params.put("rows",Integer.parseInt((String)params.get("limit")));
		  }
		    JqGridPageView<HistoricProcessInstance> page=actFlowService.finished(params);
		  int pagetotal=(int)page.getRecords();
		  PageUtils pageUtil = new PageUtils(page.getRows(), pagetotal, maxResults, Integer.parseInt(params.get("page")+""));

		  return R.ok().put("page", pageUtil);
	  }
	  /**
	   * 
	   * @Title instance
	   * @Description (读取带跟踪流程的图片)
	   * @author 颜金星
	   * @create 2017年5月15日-下午5:25:28
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @throws IOException
	   * @return void
	   * @throws
	   */
	  @ApiOperation(value = "读取带跟踪流程的图片")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/instance" , method = RequestMethod.GET)
	  public void instance(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) throws IOException {
		    Map<String, Object> params = WebUtil.getParameterMap(request);
		    actFlowService.instance(params,response);
	  }
	  /**
	   * 
	   * @Title loadByDeployment
	   * @Description (读取流程定义图片、XML资源)
	   * @author 颜金星
	   * @create 2017年6月8日-下午5:02:19
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @throws IOException
	   * @return void
	   * @throws
	   */
	  @ApiOperation(value = "读取流程定义图片、XML资源")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/loadByDeployment" , method = RequestMethod.GET)
	  public void loadByDeployment(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) throws IOException {
		    Map<String, Object> params = WebUtil.getParameterMap(request);
		    actFlowService.loadByDeployment(params,response);
	  }
	  
	  /**
	   * 
	   * @Title getWorkFlowURL
	   * @Description (读取工作流模块链接)
	   * @author 颜金星
	   * @create 2017年5月25日-上午10:49:20
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @throws IOException
	   * @return void
	   * @throws
	   */
	  @ApiOperation(value = "读取工作流模块链接")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/getWorkFlowURL")
	  public R getWorkFlowURL(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) throws IOException {
		  Map<String, Object> map=new HashMap<String, Object>();
		  map.put("URL", "http://"+workFlowConfig.getWorkFlowHost()+":"+workFlowConfig.getWorkFlowPort());
		  return R.ok(map);
	  }
	  /**
	   * 
	   * @Title histoicFlow
	   * @Description (描述)
	   * @author 颜金星
	   * @create 2017年5月31日-下午4:34:53
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @Param @throws IOException
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "获取工作流节点历史信息")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/histoicFlow",method = RequestMethod.GET)
	  public R histoicFlow(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) throws IOException {
		  Map<String, Object> params = WebUtil.getParameterMap(request);
		  int firstResult = 1;
		  int maxResults = 10;
		  if(params.get("page")!=null){
			  firstResult=Integer.parseInt((String)params.get("page"));
		  }
		  if(params.get("limit")!=null){
			  maxResults = Integer.parseInt((String)params.get("limit"));
		  }
		  if(!params.containsKey("rows")){
			  params.put("rows",Integer.parseInt((String)params.get("limit")));
		  }
		  JqGridPageView<HistoricActivity> page=actFlowService.histoicFlow(params);
		  int pagetotal=(int)page.getRecords();

		  PageUtils pageUtil = new PageUtils(page.getRows(), pagetotal, maxResults, Integer.parseInt(params.get("page")+""));

		  return R.ok().put("page", pageUtil);
	  }
	  
}
